package com.hotel.controller;

import com.alibaba.fastjson.JSONObject;
import com.hotel.component.BaseController;
import com.hotel.component.IocContainer;
import com.hotel.entity.Option;
import com.hotel.entity.PageBean;
import com.hotel.entity.Storey;
import com.hotel.exception.EntityException;
import com.hotel.service.StoreyService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author Str
 * @Date 2021/10/28 21:25
 * @Description :楼层管理
 */
@WebServlet("/storey/*")
public class StoreyController extends BaseController {


    private StoreyService storeyService = (StoreyService) IocContainer.getBean("StoreyService");

    // 跳转到列表
    public String viewList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        return "forward:/jsp/storey/list.jsp";
    }

    // 跳转到新增页
    public String viewAddStorey(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        return "forward:/jsp/storey/addStorey.jsp";
    }

    public String getStorey(HttpServletRequest req, HttpServletResponse resp)
            throws IOException, ServletException {
        //{1}获取 id 的值
        String id = req.getParameter("id");
        Storey storey = null;
        JSONObject jsObj = new JSONObject();
        String respTxt = "";
        try {
            //{2}根据 id 获取用户
            storey = storeyService.getById(id);
            //{3}存入相关信息
            jsObj.put("result", "success");
            jsObj.put("storey", storey);
            respTxt = jsObj.toString();
        } catch (Exception e) {
            e.printStackTrace();
            respTxt = packErrMsg(e); //打包错误信息
        }
        return "data:" + respTxt;
    }

    // 获取楼层和id
    public String getStoreyData(HttpServletRequest req, HttpServletResponse resp) {
        List<Option> options = new ArrayList();
        List<Storey> floors = null;
        JSONObject jsObj = null;
        try {
            jsObj = new JSONObject();
            floors = storeyService.getFloors();
            // 将楼层和id填充进Option列表
            for (Storey floor : floors) {
                Option op = new Option("" + floor.getFloor(), floor.getId());
                options.add(op);
            }
            jsObj.put("result", "success");
            jsObj.put("options", options);
        } catch (SQLException e) {
            e.printStackTrace();
            jsObj.put("result", "failed");
        }
        return "data:" + jsObj;
    }

    // 获取楼层
    public String getFloorData(HttpServletRequest req, HttpServletResponse resp) {
        List<Option> options = new ArrayList();
        List<Storey> floors = null;
        JSONObject jsObj = null;
        try {
            jsObj = new JSONObject();
            floors = storeyService.getFloors();
            // 将楼层和id填充进Option列表
            for (Storey floor : floors) {
                Option op = new Option("" + floor.getFloor(), "" + floor.getFloor());
                options.add(op);
            }
            jsObj.put("result", "success");
            jsObj.put("options", options);
        } catch (SQLException e) {
            e.printStackTrace();
            jsObj.put("result", "failed");
        }
        return "data:" + jsObj;
    }

    //任务(4)-新增 or 更新一个用户。
    //映射地址: /storey/saveStorey
    public String saveStorey(HttpServletRequest req, HttpServletResponse resp) {
        Storey storey = makeStorey(req);
        System.out.println("room============" + storey);
        JSONObject jsObj = new JSONObject();
        String respTxt = "";
        try {
            storeyService.save(storey);
            jsObj.put("result", "success");
            respTxt = jsObj.toString();
        } catch (Exception e) {
            e.printStackTrace();
            respTxt = packErrMsg(e); //打包错误信息
        }
        return "data:" + respTxt;
    }

    // 将返回的数据封装成对象
    public Storey makeStorey(HttpServletRequest req) {
        System.out.println("+----------------------------------------------------+");
        System.out.println(req.getParameter("floor"));
        System.out.println("+----------------------------------------------------+");
        Storey storey = new Storey();
        //{1}修改时使用。
        storey.setId(req.getParameter("id"));
        //{2}新增时使用。
        storey.setFloor(parseInteger(req.getParameter("floor")));
        storey.setDescript(req.getParameter("descript"));
        return storey;
    }


    public String delStorey(HttpServletRequest req, HttpServletResponse resp) {
        JSONObject jo = new JSONObject();
        String id = req.getParameter("id");
        String respTxt = "";
        try {
            storeyService.delete(id);
            jo.put("result", "success");
            respTxt = jo.toString();
        } catch (SQLException | EntityException e) {
            e.printStackTrace();
            respTxt = packErrMsg(e); //打包错误信息
        }
        return "data:" + respTxt;
    }

    /**
     * 映射地址: /Notify/jsonList
     * 提交参数: page, limit, ...
     *
     * @return java.lang.String
     */
    public String jsonList(HttpServletRequest req, HttpServletResponse resp)
            throws IOException, ServletException {
        //{1}获取前端提交的关键字 [调BaseController的方法]
        Map<String, Object> keyWords = getKeyWords(req);
        for (String s : keyWords.keySet()) {
            System.out.println("key::" + s + ",value::" + keyWords.get(s));
        }
        String respTxt = "";
        PageBean<Storey> page = null;
        try {
            //{2}调用 service 方法查询数据(传入关键字)
            page = storeyService.getList(keyWords);
            //{3}转成 json 格式数据.
            respTxt = page.toJSON();
        } catch (Exception e) {
            e.printStackTrace();  //包装错误信息..
            respTxt = packErrMsg(e);
        }
        //{4}返回数据到 LayUI 表格组件。
        return "data:" + respTxt;
    }

    private String packErrMsg(Exception e) {
        JSONObject jsObj = new JSONObject();
        jsObj.put("code", -1);
        jsObj.put("cause", e.getMessage());
        jsObj.put("result", "failed");
        //{ps}如果还有其它后面再加...
        return jsObj.toString();
    }
}
